import urllib.request as ur
import lxml.etree as le
import re
import random
import time
import code
import user_agent
from selenium import webdriver
# cookie = ''
driver = webdriver.Chrome(
            executable_path='D:\Google\Chrome\Application\chromedriver.exe',
        )
driver.get('https://www.freeip.top/')
driver_cookies = driver.get_cookies()
# print(driver_cookies)
cookie_str = ''
for driver_cookie in driver_cookies:
     name = driver_cookie['name']
     value = driver_cookie['value']
     cookie_str+=f'{name}={value}; '
# print(cookie_str)

ipp_s = []
host_s = []
ip_s = []
data_s = []
# t = 2

if cookie_str:

        request = ur.Request(
            url='https://www.freeip.top/',
            headers={
                'User-Agent':user_agent.get_user_agent_pc(),
                'Cookie':cookie_str
            }
        )
        # time.sleep(0.1)
        response = ur.urlopen(request).read()
        # print(response)

# # print(str(response))
        html_x =le.HTML(response)
# # # print(html_x)
        ip_s+=html_x.xpath("//tbody//td[1]//text()")
        host_s+=html_x.xpath("//tbody//td[2]//text()")
        # print(ip_s)
for i in ip_s:
    data_s.append(
        {
            'ip':i,
        }
    )
# print(ip_s)
for i in range(len(host_s)):
    data_s[i]['host'] = host_s[i]
# print(data_s)
for data in data_s:
    ipp_s.append(f'{data["ip"]}:{data["host"]}')
if ip_s:
    def get_daili_ip():
        return random.choice(ipp_s)
    driver.quit()
# print(get_daili_ip())